package com.hellpro.lolbox.home.weight;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.db.CollectDao;
import com.hellpro.lolbox.home.collections.CollectIfro;
import com.hellpro.lolbox.home.collections.CollectInformationDowm;
import com.hellpro.lolbox.utils.UIManger;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;

public class ZhanJIChaXunActivity extends LolBaseActivity {


    @BindView(R.id.zhan_ji_web)
    WebView zhanJiWeb;
    @BindView(R.id.zhanji_toolbar)
    Toolbar zhanjiToolbar;
    private String path;


    @Override
    public int getLayoutId() {
        return R.layout.activity_zhan_jicha_xun;
    }

    @Override
    public void initView() {
        //b:加上这两句基本上就可以做到屏幕适配了
        zhanJiWeb.getSettings().setUseWideViewPort(true);
        zhanJiWeb.getSettings().setLoadWithOverviewMode(true);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String qu = intent.getStringExtra("qu");
        path = UrlConfigs.HOMEZJCX + qu + "&pn" + "=" + name;
        setBar();
        zhanJiWeb.loadUrl(path);

        zhanJiWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);

                return super.shouldOverrideUrlLoading(view, url);

            }
        });
        //在这里进行对应json数据的下载
        CollectDao collectDao = new CollectDao(this);
        SQLiteDatabase db = collectDao.getDb();
        Cursor colloct_infro = db.query("colloct_infro", new String[]{"name"}, null, null, null, null, null, null);
        boolean isCanDow =true;
        if (colloct_infro!=null){
            while (colloct_infro.moveToNext()){
                String aim = colloct_infro.getString(colloct_infro.getColumnIndex("name"));
                if (aim.equals(name)){
                    isCanDow =false;
                    return;
                }
            }
        }else {
            down(name, qu);
        }
if (isCanDow) {
    down(name, qu);
}
    }

    private void down(String name,String qu){
        CollectInformationDowm collectInformationDowm = new CollectInformationDowm(ZhanJIChaXunActivity.this);
        collectInformationDowm.downData(name, qu);
    }


    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && zhanJiWeb.canGoBack()) {
            zhanJiWeb.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

//设置toolbar
    private  void setBar(){
        zhanjiToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (zhanJiWeb.canGoBack()){
                    zhanJiWeb.goBack();
                }else {
                    finish();
                }
            }
        });
    zhanjiToolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.box_actionbar_more_icon));
        zhanjiToolbar.inflateMenu(R.menu.toobar);

        //添加menu点击事件
        zhanjiToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case  R.id.tooba_shouchang:
                        UIManger.startToActivity(ZhanJIChaXunActivity.this, CollectIfro.class);
                        break;
                    case R.id.tooba_tjshouchang:
                        Intent intent = new Intent(ZhanJIChaXunActivity.this, HomeZhanJIActivity.class);
                        intent.putExtra("type",true);
                        startActivity(intent);
                        break;
                    case R.id.tooba_fx:
                        String url = zhanJiWeb.getUrl();
                        new ShareAction(ZhanJIChaXunActivity.this).withTargetUrl(url).withTitle("LOL盒子")
                                .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                                .setCallback(umShareListener).open();
                }
                return false;
            }
        });


    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {

            Toast.makeText(ZhanJIChaXunActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ZhanJIChaXunActivity.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if(t!=null){
                Log.d("throw","throw:"+t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ZhanJIChaXunActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

}
